
app.controller('accountController', function ($scope, ngDialog, $http, Notification) {
    $scope.showAccount = function () {
        $http({
            method: 'GET',
            url: 'account/list.query'
        }).success(function (resp) {
            if (resp.status == 0) {
                $scope.accounts = resp.data.accounts;
            } else {
                Notification.error({message: resp.errMsg});
            }
        });
    }
    $scope.showAccount();

    $scope.openCreateOrUpdateDialog = function (request) {
        var bIsUpdate = true;
        if (request == null) {
            request = {
                admin: false
            };
            bIsUpdate = false;
        } else {
            if (request.admin) {
                Notification.error({message: '管理员账号禁止操作'});
                return;
            }
        }
        $http({
            method: 'GET',
            url: 'cluster/list.query'
        }).success(function (resp) {
            if (resp.status == 0) {
                console.log(resp);
                ngDialog.open({
                    preCloseCallback: function (value) {
                        $scope.showAccount();
                    },
                    template: 'accountModifyDialog',
                    controller: 'accountModifyDialogController',
                    data: {
                        adminFlag: false,
                        accountRequest: request,
                        allClusterNameList: Object.keys(resp.data.clusterInfo.clusterAddrTable),
                        allBrokerNameList: resp.data.clusterInfo.brokerAddrTable,
                        bIsUpdate: bIsUpdate
                    }
                });
            }
        });
    }

    $scope.openAddDialog = function () {
        $scope.openCreateOrUpdateDialog(null);
    }

    $scope.openUpdateDialog = function (account) {
        $scope.openCreateOrUpdateDialog(account);
    }

    $scope.deleteAccount = function (account) {
        if (account.admin) {
            Notification.error({message: '管理员账号禁止操作'});
            return;
        }
        $http({
            method: 'POST',
            url: 'account/deleteAccount.do',
            params: {
                brokerAddr: account.brokerAddr,
                accessKey: account.accessKey
            }
        }).success(function (resp) {
            if (resp.status == 0) {
                Notification.info({message: 'delete success!'});
                $scope.showAccount();
            } else {
                Notification.error({message: resp.errMsg});
            }
        });
    }
});

app.controller('accountModifyDialogController', function ($scope, $http, Notification) {
    $scope.postAccountRequest = function (accountRequestItem) {
        if (typeof(accountRequestItem.groupPerms) == 'string') {
            accountRequestItem.groupPerms = accountRequestItem.groupPerms.split(',');
        }
        if (typeof(accountRequestItem.topicPerms) == 'string') {
            accountRequestItem.topicPerms = accountRequestItem.topicPerms.split(',');
        }
        console.log(accountRequestItem);
        var request = JSON.parse(JSON.stringify(accountRequestItem));
        console.log(request);
        $http({
            method: 'POST',
            url: 'account/createOrUpdate.do',
            data: request
        }).success(function (resp) {
            if (resp.status == 0) {
                Notification.info({message: 'success!'});
                $scope.closeThisDialog('Cancel');
            } else {
                Notification.error({message: resp.errMsg});
            }
        });
    }
});